<template>
    <div id="testscore">
        <el-breadcrumb class="common-bread">
            <el-breadcrumb-item>体测成绩</el-breadcrumb-item>
            <span class="phy-standard fr" @click="checkStandard()">体测标准</span>
        </el-breadcrumb>

        <div class="select-head">
            <div class="select-head-top">
                <span class="select-head-title">学生信息：</span>
                <el-select v-if="user.userType == 0" v-model="schoolId" filterable clearable placeholder="请选择学校">
                    <el-option v-for='item in schoolList' :label='item.schoolName' :value='item.schoolId' :key="item.schoolId">
                    </el-option>
                </el-select>

                <el-select v-model="grade" placeholder="请选择年级">
                    <el-option label="全部年级" value='0'></el-option>
                    <el-option v-for='item in grades' v-if="schoolType.indexOf(item.type) > -1" :label='item.gradeName' :value='item.grade' :key="item.grade"></el-option>
                </el-select>
                <el-select v-model="classId" placeholder="请选择班级">
                    <el-option label="全部班级" value='0'></el-option>
                    <el-option v-for='item in classes' :label='item.className' :value='item.classId' :key="item.classId">
                    </el-option>
                </el-select>
            </div>
            <div>
                <span class="select-head-title">体测信息：</span>
                <el-select v-model="physicalId" placeholder="请选择体测">
                    <el-option v-for='item in physicalList' :label='item.physicalName' :value='item.physicalId' :key="item.physicalId">
                    </el-option>
                </el-select>

                <!-- <el-select class="scolevel" v-model="scoreLevel" multiple>
                    <el-option label='优秀' value='1'></el-option>
                    <el-option label='良好' value='2'></el-option>
                    <el-option label='及格' value='3'></el-option>
                    <el-option label='不及格' value='4'></el-option>
                    <el-option label='未完成' value='-99999'></el-option>
                </el-select> -->
            </div>
        </div>
        <div class="contentBox">
            <div class="list-con">
                <div class="list_head">
                    <span class="head_name">
                        <el-input type='text' placeholder="请输入学生姓名或者学籍号" v-model="keywords" @keyup.enter.native="search">
                            <!-- <el-button slot="append" icon="el-icon-search"></el-button> -->
                        </el-input>
                    </span>
                    <el-button class="search_btn" type="primary" @click="getScoreList(1)">搜索</el-button>
                    <el-button class="fr" type="primary" @click="exportPhysicalScore">导出成绩</el-button>
                    <el-button class="fr" v-if="user.userType == 0" type="primary" @click="scoreImport">导入成绩</el-button>
                </div>
                <el-table :data="dataList" :highlight-current-row="true" @row-dblclick="skipScoreDetail">
                    <el-table-column type="index" label="序号"></el-table-column>
                    <el-table-column prop="name" label="学生名称"></el-table-column>
                    <el-table-column prop="gender" label="性别"></el-table-column>
                    <el-table-column prop="campusId" label="学籍号" width="170"></el-table-column>
                    <el-table-column prop="grade" label="年级"></el-table-column>
                    <el-table-column prop="className" label="班级"></el-table-column>
                    <el-table-column prop="totalScore" label="标准分"></el-table-column>
                    <el-table-column prop="extraScore" label="附加分"></el-table-column>
                    <el-table-column label="体测总分">
                        <template slot-scope="scope">
                            <span v-if="scope.row.isRed" class="red">{{scope.row.computerTotal}}</span>
                            <span v-else>{{scope.row.computerTotal}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="scoreLevel" label="体测成绩">
                        <template slot-scope="scope">
                            <span v-if="scope.row.isRed" class="red">{{scope.row.scoreLevel}}</span>
                            <span v-else>{{scope.row.scoreLevel}}</span>
                        </template>
                    </el-table-column>
                    <el-table-column width="100" align="right">
                        <template slot-scope="scope">
                            <i class="el-icon-document blueIcon" @click="skipScoreDetail(scope.row)" title="查看详情"></i>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination">
                    <el-pagination v-show="totalCount > 0" layout="prev, pager, next, jumper" :page-size="pageSize" :current-page="pageNum" :total="totalCount"
                        @current-change="handleCurrentChange">
                    </el-pagination>
                </div>
            </div>
        </div>

    </div>
</template>

<script>
    import { mapState, mapActions } from "vuex";
    export default {
        data() {
            return {
                isHavePhysicalId: true,
                dataList: [],
                keywords: '',
                schoolId: '',
                schoolType: '',
                schoolList: [],
                physicalId: '',
                physicalList: [],
                grade: '0',
                grades: [
                    { grade: '1', gradeName: '一年级', type: 1 },
                    { grade: '2', gradeName: '二年级', type: 1 },
                    { grade: '3', gradeName: '三年级', type: 1 },
                    { grade: '4', gradeName: '四年级', type: 1 },
                    { grade: '5', gradeName: '五年级', type: 1 },
                    { grade: '6', gradeName: '六年级', type: 1 },
                    { grade: '7', gradeName: '初一', type: 2 },
                    { grade: '8', gradeName: '初二', type: 2 },
                    { grade: '9', gradeName: '初三', type: 2 },
                    { grade: '10', gradeName: '高一', type: 3 },
                    { grade: '11', gradeName: '高二', type: 3 },
                    { grade: '12', gradeName: '高三', type: 3 },
                ],
                scoreLevel: ['1', '2', '3', '4', '-99999'],
                classId: '0',
                classes: [],
                pageSize: 10,
                pageNum: 1,
                totalCount: 0,
                physicalScoreInfo: {},
                gradeNum: 1,
                classIdNum: 1
            }
        },
        watch: {
            schoolId() {
                this.gradeNum += 1;
                this.schoolType = this.schoolList.filter(item => {
                    return item.schoolId == this.schoolId
                })[0].schoolType;
                if (this.gradeNum < 3 && this.physicalScoreInfo.grade) {
                    this.grade = this.physicalScoreInfo.grade
                } else {
                    this.grade = '0';
                }
                this.physicalId = '';
                this.getSchoolPhyList();
            },
            grade() {
                if (this.grade == 0) {
                    this.classId = '0';
                    this.classes = [];
                } else {
                    this.getPhysicalClassList()
                }
            }
        },
        computed: mapState({ user: state => state.user }),
        mounted() {
            if (this.user.userType > 0) {
                this.getUserSchool();
                if (this.grade > 0) {
                    this.getPhysicalClassList(1)
                }
            } else {
                //获取学校列表
                this.getSchoolList();
            }
            if (this.$util.getStore("physicalScoreInfo") && this.$util.getStore("physicalScoreInfo") != '{}') {
                this.physicalScoreInfo = JSON.parse(this.$util.getStore("physicalScoreInfo"));
                this.keywords = this.physicalScoreInfo.keywords;
                this.scoreLevel = this.physicalScoreInfo.scoreLevel;
                this.$util.removeStore('physicalScoreInfo');
            }
        },
        methods: {
            // 成绩列表
            getScoreList(type) {
                if (!this.schoolId && this.user.userType == 0) {
                    this.$util.errorMsg('请先选择一个学校');
                    return;
                }
                if (!this.physicalId) {
                    this.$util.errorMsg('请先选择体测信息');
                    return;
                }
                if (type) {
                    this.pageNum = type * 1
                }
                this.dataList = [];
                this.$axios.post('/jlp/physical/score/search', {
                    schoolId: this.schoolId,
                    pageNum: this.pageNum,
                    pageSize: this.pageSize,
                    physicalId: this.physicalId * 1,
                    grade: this.grade * 1,
                    classId: this.classId,
                    scoreLevel: this.scoreLevel,
                    keywords: this.keywords
                }).then(response => {
                    if (response.data.error == 10000) {
                        let preData = response.data.data;
                        if (preData) {
                            this.totalCount = preData.totalCount * 1;
                            let arrlist = preData.list;
                            if (arrlist && arrlist.length > 0) {
                                for (let i = 0; i < arrlist.length; i++) {
                                    let uid = arrlist[i].uid,
                                        campusId = arrlist[i].campusId,
                                        name = arrlist[i].name,
                                        className = arrlist[i].className,
                                        gender = arrlist[i].gender == 1 ? '男' : '女',
                                        grade = this.$util.gradeFormat(arrlist[i].grade),
                                        isRed = false,
                                        totalScore = arrlist[i].totalScore / 100,
                                        extraScore = arrlist[i].extraScore / 100,
                                        scoreLevel,
                                        computerTotal;
                                    if (arrlist[i].scoreLevel == -99999) {
                                        computerTotal = '未完成'
                                    } else {
                                        computerTotal = totalScore + extraScore
                                    }
                                    switch (arrlist[i].scoreLevel * 1) {
                                        case 1:
                                            scoreLevel = '优秀';
                                            break;
                                        case 2:
                                            scoreLevel = '良好';
                                            break;
                                        case 3:
                                            scoreLevel = '及格';
                                            break;
                                        case 4:
                                            scoreLevel = '不及格';
                                            isRed = true;
                                            break;
                                        case -99999:
                                            scoreLevel = '未完成';
                                            totalScore = '未完成';
                                            isRed = true;
                                            break;
                                    }
                                    this.dataList.push({ uid, campusId, name, className, gender, computerTotal, grade, isRed, totalScore, extraScore, scoreLevel });
                                }
                            }
                        }
                    }
                });
            },

            // 成绩导入
            scoreImport() {
                location.href = '/#/phyScoreImport'
            },
            // 获取登录者学校信息
            getUserSchool() {
                this.schoolList = []
                this.$axios.get('/adminapi/v1/tools/schools/info', {
                    params: {
                        schoolId: this.user.schoolId
                    }
                }).then((res) => {
                    if (res.data.error == 10000) {
                        let pro = res.data.data;
                        let schoolType = '';
                        schoolType += pro.havePrimary ? 1 : '';
                        schoolType += pro.haveMiddle ? 2 : '';
                        schoolType += pro.haveHigh ? 3 : '';
                        this.schoolList.push({
                            schoolName: pro.name,
                            schoolId: pro.schoolId,
                            schoolType: schoolType
                        })
                        this.schoolId = pro.schoolId
                    }
                })
            },
            //获取学校列表
            getSchoolList() {
                this.schoolList = [];
                this.$util.getSchools().then(schools => {
                    schools.forEach(item => {
                        let schoolType = '';
                        schoolType += item.havePrimary ? 1 : '';
                        schoolType += item.haveMiddle ? 2 : '';
                        schoolType += item.haveHigh ? 3 : '';
                        this.schoolList.push({
                            schoolName: item.name,
                            schoolId: item.schoolId,
                            schoolType: schoolType
                        })
                    });
                    if (this.physicalScoreInfo.schoolId) {
                        this.schoolId = this.physicalScoreInfo.schoolId
                    } else {
                        this.schoolId = this.schoolList[0].schoolId || '';
                    }
                })
            },

            // 获取体测列表
            getSchoolPhyList() {
                this.physicalList.splice(0, this.physicalList.length);

                this.$axios.post('/jlp/physical/school/participate/list', {
                    schoolId: this.schoolId
                }).then(res => {
                    if (res.data.error == 10000) {
                        let arrData = res.data.data;
                        if (arrData && arrData.length) {
                            arrData.forEach(item => {
                                this.physicalList.push({
                                    physicalId: item.physicalId + '',
                                    physicalName: item.physicalName
                                })
                            })
                            // if (this.user.userType > 1) {
                            //     this.physicalId = this.physicalId > 0 ? this.physicalId : this.physicalList[0].physicalId;
                            //     this.getScoreList();
                            // } else {
                            //     this.physicalId = this.physicalList[0].physicalId;
                            //     this.getScoreList(1);
                            // }

                            if (this.physicalScoreInfo.physicalId) {
                                this.physicalId = this.physicalScoreInfo.physicalId
                            } else {
                                this.physicalId = this.physicalList[0].physicalId || '';
                            }
                            this.getScoreList(1);
                        }
                    }
                })
            },

            // 获取班级列表
            getPhysicalClassList(type) {
                this.classes = [];
                this.classIdNum += 1;
                this.$axios.get("/adminapi/v1/tools/classes/list", {
                    params: {
                        grade: this.grade,
                        schoolId: this.schoolId,
                        pageSize: 100,
                        pageNum: 1
                    }
                }).then(response => {
                    if (response.data.error == 10000) {
                        this.classes = [];
                        if (!response.data.data || !response.data.data.content) return;
                        let list = response.data.data.content;

                        list.forEach(item => {
                            let gradeName = this.$util.gradeFormat(item.grade * 1);
                            let className = gradeName + item.number + "班";
                            this.classes.push({
                                className: className,
                                classId: item.classId
                            })
                        })
                        if (this.classIdNum < 3 && this.physicalScoreInfo.classId) {
                            this.classId = this.physicalScoreInfo.classId
                        } else {
                            this.classId = '0';
                        }
                    }
                })
            },

            // 体测标准
            checkStandard() {
                location.href = '/#/physicalStandard'
            },

            // 导出成绩
            exportPhysicalScore() {
                this.$axios.post('/jlp/physical/score/export', {
                    schoolId: this.schoolId,
                    physicalId: this.physicalId * 1,
                    grade: this.grade,
                    classId: this.classId,
                    scoreLevel: this.scoreLevel
                }).then(response => {
                    if (response.data.error == 10000) {
                        location.href = response.data.data;
                    } else {
                        this.$util.errorMsg('导出失败')
                    }
                }).catch(() => { })
            },

            // 成绩详情
            skipScoreDetail(row) {
                let params = {
                    campusId: row.campusId,
                    schoolId: this.schoolId,
                    grade: this.grade,
                    classId: this.classId,
                    physicalId: this.physicalId,
                    pageNum: this.pageNum,
                    scoreLevel: this.scoreLevel
                }
                this.$util.setStore("physicalScoreInfo", JSON.stringify(params));
                this.$util.refreshTo("/#/phyScoreDetail");
            },


            handleCurrentChange(val) {
                this.pageNum = val;
                this.getScoreList()
            }
        }
    }
</script>
<style>
    #testscore .el-select:first-child {
        margin-bottom: 10px;
    }

    #testscore .contentBox {
        min-width: 1060px;
        height: calc(100% - 195px);
    }

    #testscore .list_head {
        padding-left: 0;
    }

    #testscore .red {
        color: red;
    }

    #testscore .scolevel .el-input--suffix {
        width: 405px;
    }

    #testscore .select-head-top {
        margin-bottom: 10px;
    }

    #testscore .select-head-title {
        display: inline-block;
        width: 80px;
        text-align: right;
    }

    #testscore .select-head {
        min-width: 1060px;
        background-color: #fff;
        padding: 20px 24px 10px;
        margin-left: 32px;
        margin-right: 32px;
    }
</style>